import React, { Component } from 'react';
import { Form, DatePicker, Input,Select } from 'antd';
import { isEmpty, omit } from 'lodash';
import ObjectBox from '/common/componentsTpl/ObjectBox';
import SearchComp from '/common/components/SearchComp';
import { appConfig } from '@/appConfig';
import moment from 'moment';

const Option = {Select}

export default class SearchForm extends Component {
  render() {

    return (
      <SearchComp
        getPreviewData={(formData) => {
          let name = formData.name || {};
          let dept = formData.dept || {};
          let phone = formData.phone||{};
          let type = formData.type||{};
          let enabled = formData.enabled||{};
          let code = formData.code||{}
          // 生成预览数据数组，如
          // [{
          //   label: '会计期间',
          //   fieldNames: 'beginPeriod;endPeriod',
          //   text: '2019-06-10 至 2019-06-17',
          // }]
          const previewData = [];
          if(!isEmpty(code)){
            previewData.push({
              label:'员工编号',
              fieldNames:'code',
              text: `员工编号：${formData.code}`,
            });
          }
          if (!isEmpty(name)) {
            previewData.push({
              label: '员工名称',
              fieldNames: 'name',
              text: `员工名称：${formData.name}`,
            });
          }

          if (!isEmpty(phone)) {
            previewData.push({
              label: '员工手机',
              fieldNames: 'phone',
              text: `员工手机：${formData.phone}`,
            });
          }


          if (!isEmpty(type)) {
            previewData.push({
              label: '员工类型',
              fieldNames: 'type',
              text: `员工类型：${type.name}(${type.code})`,
            });
          }

          if (!isEmpty(dept)) {
            previewData.push({
              label: '所属部门',
              fieldNames: 'dept',
              text: `所属部门：${dept.name}(${dept.code})`,
            });
          }
          if (!isEmpty(enabled)) {
            previewData.push({
              label: '开启状态',
              fieldNames: 'enabled',
              text: `开启状态：${enabled.name}`,
            });
          }
          return previewData;
        }}
        {...omit(this.props, ['onRef'])}
      >
        <SearchInForm/>
      </SearchComp>
    );
  }
}

export class SearchInForm extends Component {

  constructor(props) {
    super(props);
    this.state = {
      openStatus:[
        {id:0,name:'关'},
        {id:1,name:'开'},
        ]//检查状态
    };
  }

  componentDidMount() {
    // console.log('SearchInForm::::',this.props);
    if (this.props.onRef) this.props.onRef(this);
    // if (this.props.formData.acctYear && this.props.formData.acctYear.id > 0) {
      // this.fetchPeriod(this.props.formData.acctYear.id);
    // }

    // 如果有预设的科目，则根据 code 获取科目
    if (!isEmpty(this.props.formData.urlQuery)) {
      const urlQuery = this.props.formData.urlQuery;

    }
  }

  // componentDidUpdate(prevProps, prevState) {
  //   if (!isObjectValEqual(prevProps.formData.acctYear, this.props.formData.acctYear) && this.props.formData.acctYear && this.props.formData.acctYear.id > 0) {
  //     this.fetchPeriod(this.props.formData.acctYear.id);
  //   }
  // }

  getAjaxData(fieldsValue) {
    console.log('>>> getAjax', fieldsValue);
    const formData = fieldsValue;
    const values = {
      'refUser.code':!isEmpty(formData.code)?formData.code:'',
      'department.id':!isEmpty(formData.dept)?formData.dept.id:'',
      'refUser.name':!isEmpty(formData.name)?formData.name:'',
      'refUser.phone':!isEmpty(formData.phone)?formData.phone:'',
      'type.id':!isEmpty(formData.type)?formData.type.id:'',
      'refUser.enabled':!isEmpty(formData.enabled)?(formData.enabled.id?"true":"false"):'',//
    };
    // if(!isEmpty(formData.code)){
    //   values['refUser.code']=formData.code
    // }
    // if(!isEmpty(formData.dept)){
    //   values['department.id'] = formData.dept.id
    // }
    // if(!isEmpty(formData.name)){
    //   values['refUser.name']=formData.name
    // }
    //
    // if(!isEmpty(formData.phone)){
    //   values['refUser.phone']=formData.phone
    // }
    // if(!isEmpty(formData.type)){
    //   values['type.id'] =  formData.type.id
    // }
    // if(!isEmpty(formData.enabled)){
    //   values['refUser.enabled'] = formData.enabled.id
    // }
    return values;
  }



  render() {
    const { formData } = this.props;
    const { getFieldDecorator } = this.props.form;
    const { openStatus } = this.state;
    return (
      <>
        <>
          <Form.Item key={`code`} label='员工编码'>{
            getFieldDecorator('code', {
              initialValue: formData.code || null,
            })(
              <Input/>,
            )
          }
          </Form.Item>
          <Form.Item key={`name`} label='员工名称'>{
            getFieldDecorator('name', {
              initialValue: formData.name || null,
            })(
              <Input/>,
            )
          }
          </Form.Item>
          <Form.Item style={{ width: '100%' }} label='员工手机'>{
            getFieldDecorator('phone', {
              initialValue: formData.phone || ''
            })(
              <Input/>,
            )
          }
          </Form.Item>
        </>


        <Form.Item label='员工类型'>{
          getFieldDecorator('type', {
            initialValue: formData.type || null,
          })(
            <ObjectBox appCode={appConfig.EMPLOYEE_CATEGORY.finderCode}/>,
          )
        }
        </Form.Item>
        <Form.Item  label="所属部门">{
          getFieldDecorator('dept', {
            initialValue: formData.dept || null,
          })(
            <ObjectBox appCode={appConfig.DEPARTMENT.finderCode}/>,
          )
        }
        </Form.Item>
        <Form.Item label='开启状态'>{
          getFieldDecorator('enabled', {
            initialValue: formData.enabled || null,
          })(
            <ObjectBox compData={openStatus}
                       nativeProps={{
                         placeholder: '请选择开启状态',
                       }}
                       renderOptionText={v => (
                         `${v.name}`
                       )}
                       primaryKey={'name'}
            />,

          )
        }
        </Form.Item>



      </>
    );
  }
}
